﻿using GDGeek;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraManager : Singleton<CameraManager>
{

    bool camera_fllow_flag;

    public GameObject mainCamera;

    public GameObject player;

    public Camera baseCamera;

    public float maxCameraSize;
    public float minCameraSize;
    // Use this for initialization
    void Start()
    {
        maxCameraSize = 5;
        minCameraSize = 2;
        camera_fllow_flag = false;

        StartCoroutine(Wait_CheckFlag());
        baseCamera = this.GetComponent<Camera>();
    }

    IEnumerator Wait_CheckFlag()
    {
        yield return new WaitForSeconds(0.2f);
        player = GameObject.Find("GameMainPlayer");
        yield return new WaitForSeconds(0.1f);
        camera_fllow_flag = true;
    }


    // Update is called once per frame
    void Update()
    {

       // SetCameraSize();
    }

    void SetCameraSize()
    {
        float size = baseCamera.orthographicSize;

        if (Input.GetAxis("Mouse ScrollWheel") < 0)
        {
            if(size <= minCameraSize)
            {
                baseCamera.orthographicSize = minCameraSize;
            }
            else
            {
                baseCamera.orthographicSize -= 0.1f;
            }
        }
        //Zoom in
        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {
            if (size >= maxCameraSize)
            {
                baseCamera.orthographicSize = maxCameraSize;
            }
            else
            {
                baseCamera.orthographicSize += 0.1f;
            }
        }
    }

    public void LateUpdate()
    {

        CameraFllowPlayer();
    }


    public void Set_Flag(bool _flag)
    {
        camera_fllow_flag = _flag;
    }

    void CameraFllowPlayer()
    {
        if (camera_fllow_flag)
        {
            if (mainCamera != null)
            {
                if (player != null)
                {
                    Vector3 pox = new Vector3(player.transform.position.x, player.transform.position.y, -10f);
                    mainCamera.gameObject.transform.position = pox;
                   // Debug.Log("摄像机已经找到了，跟踪玩家中...");
                }
            }
            else
            {
                // mainCamera = GameObject.Find("Main Camera");
                mainCamera = this.gameObject;

               // Debug.Log("Null is 摄像机为空，正在查找");
            }
        }
    }
}
